package com.dsx.myblog.ThreadPool;



import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public class DefaultThreadPool<T extends Runnable> implements ThreadPools<T>{

    public DefaultThreadPool(int num){
        if(num > MAX_WORKER_NUMBERS){
            num = DEFAULT_WORKER_NUMBERS;
        } else{
            this.workerNum = num;
        }
        initializeWorkers(num);
    }


    /**
     * 线程池维护工作者线程的最大数量
     */
    private static final int MAX_WORKER_NUMBERS=30;

    /**
     * 线程池维护工作者线程的最默认工作数量
     */
    private static final int DEFAULT_WORKER_NUMBERS = 5;

    /**
     * 线程池维护工作者线程的最小数量
     */
    private static final int MIN_WORKER_NUMBERS = 1;

    /**
     * 维护一个工作列表,里面加入客户端发起的工作
     */
    private final LinkedList<com.dsx.myblog.ThreadPool.Job> jobs = new LinkedList<>();

    /**
     * 工作者线程的列表
     */
    private final List<Worker> workers = Collections.synchronizedList(new ArrayList<>());

    /**
     * 工作者线程的数量
     */
    private int workerNum;

    /**
     *每个工作者线程编号生成
     */
    private AtomicLong threadNum = new AtomicLong();




    @Override
    public void execute(Job job) {
        if(job == null){
            throw new NullPointerException();
        }
        jobs.addLast(job);
        jobs.notify();
    }

    @Override
    public void shutdown() {
        for(Worker worker : workers){
            worker.shutdown();
        }
    }

    @Override
    public void addWorker(int num) {
        synchronized (jobs){
            if(num + this.workerNum > MAX_WORKER_NUMBERS){
                num = MAX_WORKER_NUMBERS - this.workerNum;
            }
            initializeWorkers(num);
            this.workerNum += num;
        }
    }

    private void initializeWorkers(int num){
        for (int i = 0; i < num; i++) {
            Worker worker =  new Worker();
            workers.add(worker);
            Thread thread = new Thread(worker);
            thread.start();
        }
    }

    @Override
    public void removeWorker(int num) {
        if(num > workers.size()){
            throw new IllegalArgumentException("超过了已有的线程数量");
        }
        for(int i=0; i< num; i++){
            Worker worker = workers.get(i);
            if(worker != null){
                worker.shutdown();
                workers.remove(i);
            }
        }
        this.workerNum -= num;
    }

    @Override
    public int getJobSize() {
        return workers.size();
    }



    class Worker implements Runnable{

        private volatile boolean running = true;

        @Override
        public void run() {
            while (running){
                com.dsx.myblog.ThreadPool.Job job;
                synchronized(jobs){
                    if(jobs.isEmpty()){
                        try{
                            jobs.wait();
                        } catch (InterruptedException e){
                            Thread.currentThread().interrupt();
                           // Thread.sleep();
                            return;
                        }
                    }
                    job = jobs.removeFirst();
                }
                if(job != null){
                    job.run();
                }
            }
        }

        public void shutdown(){
            running = false;
        }
    }
}

